//
//  ICPullComboBoxView.m
//  ICome
//
//  Created by zhangrongwu on 16/3/30.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICPullComboBoxView.h"
#import "ICCommonGroup.h"
#import "ICHeaderFile.h"

#define margin  6
@interface ICPullComboBoxView ()
@property (nonatomic, assign)BOOL comBoxStatus;
@property (nonatomic, strong)UIView *comBackView;
@property (nonatomic, strong)UIImageView *comBackImageView;
@property (nonatomic, strong)UIImageView *cornerImageView;
@end
@implementation ICPullComboBoxView
- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = SEARCHBACKGROUNDCOLOR;
        [self addTap];
        
        self.comBoxStatus = NO;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self initNotificationCenter];
        
    }
    return self;
}
-(void)layoutSubviews {
    [super layoutSubviews];
    [self.cornerImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.tableView.mas_top).offset(1);
        make.right.equalTo(self.tableView.mas_right).offset(-20);
    }];
}
- (void)initNotificationCenter {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(interfaceReloadData:) name:NotificationChangeUIView object:nil];
}

- (void)interfaceReloadData:(NSNotification *)notification {
    [self.tableView reloadData];
}

- (void)addTap {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTap)];
    [self.comBackView addGestureRecognizer:tap];
}

- (void)viewTap {
    if (self.comBoxStatus) {
        [self close];
    } else {
        [self open];
    }
}
- (void)hidePubBoxView {
    [self close];
}

-(void)close {
    [UIView animateWithDuration:0.2 animations:^{
        self.tableView.frame = CGRectMake(App_Frame_Width - 130.0f - margin, 10, 130.0f , 0);
        self.alpha = 0;
    }];
    self.comBoxStatus = NO;
}

-(void)open {
    if (self.dataList.count > 0) {
        [UIView animateWithDuration:0.2 animations:^{
            ICCommonGroup *group = [self.dataList objectAtIndex:0];
            self.tableView.frame = CGRectMake(App_Frame_Width - 130.0f - margin, 10, 130.0f, group.items.count * 44 * Level);
            self.alpha = 1;
        }];
        self.comBoxStatus = YES;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    ICCommonGroup *group = [self.dataList objectAtIndex:section];
    return group.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"ICPullComBoxCell";
    ICPullComBoxCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[ICPullComBoxCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    ICCommonGroup *group = [self.dataList objectAtIndex:indexPath.section];
    cell.item = [group.items objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    cell.rightFreeSpace = 15;
    cell.titleLabel.font = [UIFont systemFontOfSize:14];
    cell.titleLabel.textColor = ICRGB(0x585858);

    if (indexPath.row == group.items.count - 1) {
        cell.bottomLine.alpha = 0;
    } else {
        cell.bottomLine.alpha = 0.4;
    }
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44 * Level;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.dataList.count > 0) {
        [self close];
        if ([self.delegate respondsToSelector:@selector(didSelectRowAtIndexPath:)]) {
            [self.delegate didSelectRowAtIndexPath:indexPath];
        }
    }
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundView = self.comBackImageView;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.scrollEnabled = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.frame = CGRectMake(App_Frame_Width - 130.0f - margin, 10, 130.0f, 44 * Level);
        [self addSubview:_tableView];
    }
    return _tableView;
}

- (UIView *)comBackView {
    if (!_comBackView) {
        _comBackView = [[UIView alloc] initWithFrame:self.frame];
        [self addSubview:_comBackView];
    }
    return _comBackView;
}

- (UIImageView *)comBackImageView {
    if (!_comBackImageView) {
        _comBackImageView = [[UIImageView alloc] initWithFrame:self.tableView.frame];
        _comBackImageView.image = [UIImage imageNamedFromiComeSDKBundle:@"icon_boxback"];
    }
    return _comBackImageView;
}

-(UIImageView *)cornerImageView {
    if (!_cornerImageView) {
        _cornerImageView = [[UIImageView alloc] init];
        _cornerImageView.image = [UIImage imageNamedFromiComeSDKBundle:@"icon_boxup"];
        [self addSubview:_cornerImageView];
    }
    return _cornerImageView;
}

- (NSMutableArray *)dataList {
    if (!_dataList) {
        _dataList = [[NSMutableArray alloc] init];
    }
    return _dataList;
}

@end
